package com.tianci.appstore.ui.list;

import android.content.Context;
import android.view.Gravity;
import android.view.View;

import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyAppDetailData.AppStatus;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.data.SkyListProgressData;
import com.tianci.appstore.ui.widget.ProgressBar;
import com.tianci.appstore.utils.SkyConstants;

/**
 * 列表页中的app块View
 * 使用此view的列表有：1.下载管理列表、2.更新管理列表
 **/
public class ListProgressBlock extends ListStatusBlock {
    private ProgressBar progress;

    public ListProgressBlock(Context context, ListType listType) {
        super(context, listType);
        // TODO Auto-generated constructor stub
        progress = new ProgressBar(context);
        progress.setProgressDrawable(R.drawable.progressbar);
        SkinManager.setBackground(progress, R.drawable.progressbar_bg);
        LayoutParams progressParams = new LayoutParams(LayoutParams.MATCH_PARENT, SkyConstants.Div(6), Gravity.BOTTOM);
        progressParams.setMargins(SkyConstants.Div(9), 0, SkyConstants.Div(9),
                SkyConstants.Div(15));
        this.addView(progress, progressParams);
    }

    @Override
    public void update(SkyAppBaseData data) {
        // TODO Auto-generated method stub
        super.update(data);
        AppStatus status = ((SkyListProgressData) data).getStatus();
        updateStatus(status);
    }

    @Override
    public void updateStatus(AppStatus status) {
        // TODO Auto-generated method stub
        super.updateStatus(status);
        if (status == AppStatus.DOWNLOAD_PAUSED || status == AppStatus.DOWNLOADING) {
            progress.setVisibility(View.VISIBLE);
            updateProgress(((SkyListProgressData) data).getPercent());
        } else
            progress.setVisibility(View.INVISIBLE);
    }

    public void updateProgress(int percent) {
        if (status == AppStatus.DOWNLOAD_PAUSED || status == AppStatus.DOWNLOADING) {
            ((SkyListProgressData) data).setPercent(percent);
            progress.setVisibility(View.VISIBLE);
            progress.setProgress(percent);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (inSelectionMode) {
            super.onClick(v);
        } else {
            if (data == null)
                return;
            if (clickListener != null)
                clickListener.onBlockClicked(this, data, this.mIndex);
        }
    }

    @Override
    public void clearData() {
        // TODO Auto-generated method stub
        super.clearData();
        progress.setVisibility(View.INVISIBLE);
    }
}
